-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


local entrynode = nil;
local datalock = false;

function onInit()
	-- Prepare the node holding group entries
	entrynode = window.getDatabaseNode().createChild("entries");
	entrynode.onChildAdded = onChildAdded;
	for k,v in pairs(entrynode.getChildren()) do
		createSourcedWindow(v);
	end

	registerMenuItem("Create Item", "insert", 5);
end

function onMenuSelection(selection)
	if selection == 5 then
		createSourcedWindow();
	end
end

function onChildAdded(parent, child)
	if datalock then
		return;
	end

	for k,v in pairs(getWindows()) do
		if child == v.getDatabaseNode() then
			return
		end
	end

	createSourcedWindow(child);
end

function refreshSort()
	applySort();
end

function onSortCompare(w1, w2)
	if w1.name.getValue() == "" then return true; end
	if w2.name.getValue() == "" then return false; end
	return string.upper(w1.name.getValue()) > string.upper(w2.name.getValue());
end

function onDrop(x, y, draginfo)
	if draginfo.isType("combattrackerentry") then
		-- Add group to group move here
	elseif draginfo.isType("playercharacter") or draginfo.isType("shortcut") then
		local dropclass, dropdatasource = draginfo.getShortcutData();
		local trackerclass = CombatManager.getTrackerClass(dropclass);
		
		if trackerclass then
			datalock = true;
			window.windowlist.window.setDataClassHint(trackerclass);
			local w = createNewTrackerWindow(trackerclass);
			datalock = false;

			if w.populate then
				w.populate(DB.findNode(dropdatasource), nil);
			end
		end
		
		return true;
	end
end

function createSourcedWindow(datanode)
	datalock = true;
	local datasource = datanode or entrynode.createChild();
	datalock = false;

	-- Determine class of window to create
	local linknode = datasource.getChild("link");
	local trackerclass = window.windowlist.window.getDataClassHint();
	
	if linknode then
		trackerclass = CombatManager.getTrackerClass(linknode.getValue());
	end
	
	-- Create
	if trackerclass then
		return createWindowWithClass(trackerclass, datasource);
	end

	local defaulttype = CombatManager.getDefaultTrackerType();
	if defaulttype then
		return createWindowWithClass(defaulttype, datasource);
	else
		return createWindow(datasource);
	end
end

function createNewTrackerWindow(trackerclass)
	datalock = true;
	local datasource = datanode or entrynode.createChild();
	datalock = false;

	return createWindowWithClass(trackerclass, datasource);
end
